#include "openssl_util/openssl_util.h"
#include "string_util/string_util.h"

#include <cstdint>
#include <cstdio>
#include <gtest/gtest.h>
#include <openssl/evp.h>
#include <openssl/sha.h>
#include <string>
#include <vector>

TEST(MD5SUM, Test)
{
    std::string content = "cctv10";
    std::string md5_result;
    OpenSSLUtil::EncodeDataToMD5(content, md5_result);
    printf("%s.\n", md5_result.c_str());
}

TEST(SHA256SUM, Test)
{
    std::string content = "cctv10";
    std::string sha256;
    OpenSSLUtil::EncodeDataSHA256(content, sha256);
    printf("%s.\n", sha256.c_str());
}

TEST(SHA1SUM, Test)
{
    std::string content = "cctv10";
    std::string sha1;
    OpenSSLUtil::EncodeDataSHA1(content, sha1);
    printf("%s.\n", sha1.c_str());
}

TEST(MessageIntegerity, Sha1)
{
    std::vector<std::string> test = {
        "0001004c2112a44248497667515a59676a4a503200060009626f61723a63436e6e000000c0570004000003e7802900088362856299c55f6b002400046e001eff000800142c9995cf40cbeebedc5d367a59bec4776b0082d9802800047ad9c7a2",
        "000100442112a44248497667515a59676a4a503200060009626f61723a63436e6e000000c0570004000003e7802900088362856299c55f6b002400046e001eff00080014",
        "000100442112a44248497667515a59676a4a503200060009626f61723a63436e6e000000c0570004000003e7802900088362856299c55f6b002400046e001eff",
        "000100442112a44248497667515a59676a4a503200060009626f61723a63436e6e000000c0570004000003e7802900088362856299c55f6b002400046e001eff000000000000000000000000000000000000000000000000"};
    for (auto & content : test)
    {
        std::string          sha1;
        std::vector<uint8_t> c;
        StringUtil::HexStrToCharacter(content, c);
        OpenSSLUtil::EncodeDataSHA1(c.data(), c.size(), sha1);
        printf("%s.\n", sha1.c_str());
    }
}

TEST(MD5Test, Key)
{
    std::string content = "user:realm:pass";
    std::string result;
    OpenSSLUtil::EncodeDataToMD5(content, result);
    printf("md5 result : %s.\n", result.c_str());
}

TEST(MessageIntergrity, Test)
{
    // V
    std::string content = "00010050";
    // V
    content            += "2112a442";
    // V
    content            += "b7e7a701";
    // V
    content            += "bc34d686";
    // V
    content            += "fa87dfae";
    // V
    content            += "80220010";
    // V
    content            += "5354554e";
    // V
    content            += "20746573";
    // V
    content            += "7420636c";
    // V
    content            += "69656e74";
    // V
    content            += "00240004";
    // V
    content            += "6e0001ff";
    // V
    content            += "80290008";
    // V
    content            += "932ff9b1";
    // V
    content            += "51263b36";
    // V
    content            += "00060009";
    // V
    content            += "6576746a";
    // V
    content            += "3a683676";
    //
    content            += "59202020";
    //	content            += "00080014";
    //	content            += "9aeaa70c";
    //	content            += "bfd8cb56";
    //	content            += "781ef2b5";
    //	content            += "b2d3f249";
    //	content            += "c1b571a2";
    //	content            += "80280004";
    //	content            += "e57a3bcf";
    std::vector<uint8_t> hex_data;
    StringUtil::HexStrToCharacter(content, hex_data);
    std::string key = "VOkJxbRl1RmTxUk/WvJxBt";
    std::string result;
    OpenSSLUtil::EncodeHMACSHA1(hex_data.data(), hex_data.size(), key, result);
    printf("result : %s.\n", result.c_str());
}
